package com.lsx.logisticscore.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logistics.response.CommonResp;
import com.lsx.logisticscore.entity.Car;
import com.lsx.logisticscore.mapper.CarMapper;
import com.lsx.logisticscore.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;

    @Autowired
    private CarMapper carMapper;

    @RequestMapping("/add")
    public CommonResp<Integer> addCar(@RequestBody String params){
        log.info("$$$$$$$$ 进入 addCar ：{}", params);
        Car car = JSONObject.parseObject(params, Car.class);
        log.info("$$$$$$$$ 解析后的Car ：{}", car);
        int result = carService.insertCar(car);
        CommonResp<Integer> resp = new CommonResp<>();
        if (result == -1){
            resp.setStatus("-1");
            resp.setMessage("manager不存在！");
        }else if (result == -2){
            resp.setStatus("-2");
            resp.setMessage("manager类型错误！");
        }
        resp.setContent(result);
        return resp;
    }

    @PostMapping("/remove")
    public  CommonResp<Integer> removeCar(@RequestBody String carId){
        carId = JSONObject.parseObject(carId).getString("carId");
        log.info("$$$$$$$$$$$$$$carId = {}",carId);
        int result = carMapper.deleteById(Integer.valueOf(carId));
        CommonResp<Integer> resp = new CommonResp<>();
        if (result < 1){
            resp.setStatus("-1");
            resp.setMessage("删除失败！或许已不存在");
        }
        resp.setContent(result);
        return resp;
    }

    @RequestMapping("/update")
    public  CommonResp<String> updateCar(@RequestBody String params){
        Car car = JSONObject.parseObject(params, Car.class);
        log.info("$$$$$$$$$$$$ update car = {}", car);
        String result = carService.updateCar(car);
        CommonResp<String> resp = new CommonResp<>();
        if ("-1".equals(result)){
            resp.setStatus("-1");
            resp.setMessage("manager不存在！");
        }else if ("-2".equals(result)){
            resp.setStatus("-2");
            resp.setMessage("manager类型错误！");
        }
        resp.setContent(result);
        return resp;
    }

    @RequestMapping("/list")
    public  CommonResp<Page<Car>> listCar(Page<Car> page, String carType, String managerId){
        if ("".equals(carType)){
            carType = null;
        }
        if ("".equals(managerId)){
            managerId = null;
        }
        return new CommonResp<>(carMapper.selectCarByCondition(page, carType, managerId));
    }

    @RequestMapping("/{carId}")
    public CommonResp<Car> getCarById(@PathVariable Integer carId){
        return new CommonResp<>(carMapper.selectById(carId));
    }

    @RequestMapping("/getCar/{empId}")
    public CommonResp<List<Car>> getCarByEmpId(@PathVariable String empId){
        Map<String, Object> param = new HashMap<>();
        param.put("manager_id", empId);
        return new CommonResp<>(carMapper.selectByMap(param));
    }

    @RequestMapping("/getCarType")
    public CommonResp<List<String>> getCarType(){
        return new CommonResp<>(carMapper.selectCarType());
    }

    @RequestMapping("/getDrivers")
    public CommonResp<List<Map<String, Object>>> getDrivers(){
        return new CommonResp<>(carMapper.selectDrivers());
    }
}
